/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.Tag;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.ui.view.FollowButton;
import com.simon.mengkou.ui.view.flowlayout.FlowLayout;
import com.simon.mengkou.utils.UtilOuer;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/22.
 * desc    :
 */
public class ProductOrderAdapter extends AbsAdapter<Follow> {

    public ProductOrderAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_product_order_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Follow data = getItem(position);

        final User user = data.getUser();
        //头像
        holder.mSdvAvatar.loadAvatar(user.getAvatarUrl(), user.getLevel());
        holder.mSdvAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开用户详情
                OuerDispatcher.presentUserActivity(mContext, user.getUserId(), user.getNick());
            }
        });

        //标题
        holder.mTvName.setText(user.getNick());
        //时间
        holder.mTvTime.setText(UtilOuer.getFormatDate(mContext, data.getCreateAt(), "yy-M-d"));
        //内容
        holder.mTvContent.setText(data.getContent());
        //图片
        List<Image> images = data.getImageArray();
        int count = UtilList.getCount(images);
        if(count == 0) {
            holder.mLlRoot.setVisibility(View.GONE);
        } else {
            holder.mLlRoot.setVisibility(View.VISIBLE);
            if(count == 1) {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage0, images.get(0).getUrl());
            } else if(count == 2){
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage0, images.get(0).getUrl());
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage1, images.get(1).getUrl());
            } else {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage0, images.get(0).getUrl());
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage1, images.get(1).getUrl());
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage2, images.get(2).getUrl());
            }

            holder.mLlRoot.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    //晒单详情
                    OuerDispatcher.presentShareOrderDetailActivity(mContext, data.getId());
                }
            });
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //头像
        @Bind(R.id.order_id_avatar)
        AvatarSimpleDraweeView mSdvAvatar;
        //标题
        @Bind(R.id.order_id_name)
        TextView mTvName;
        //时间
        @Bind(R.id.order_id_time)
        TextView mTvTime;
        //内容
        @Bind(R.id.order_id_content)
        TextView mTvContent;
        //
        @Bind(R.id.product_order_id_image_root)
        LinearLayout mLlRoot;
        //图片0
        @Bind(R.id.product_order_id_image0)
        SimpleDraweeView mSdvImage0;
        //图片1
        @Bind(R.id.product_order_id_image1)
        SimpleDraweeView mSdvImage1;
        //图片2
        @Bind(R.id.product_order_id_image2)
        SimpleDraweeView mSdvImage2;
    }
}
